<template>
  <div class="vehicle-detail-container">
    <div class="detail-card">
      <!-- 头部标题 -->
      <div class="header">
        <h2>车辆出入记录详情</h2>
      </div>

      <div class="content-wrapper">
        <!-- 左侧信息区域 -->
        <div class="info-section">
          <!-- 车牌号和车辆类型 -->
          <div class="plate-section">
            <div class="plate-number">
              <span class="plate-prefix">{{ vehicleInfo.platePrefix }}</span>
              <span class="plate-main">{{ vehicleInfo.plateNumber }}</span>
            </div>
            <el-tag type="warning" class="vehicle-type-tag">
              {{ vehicleInfo.vehicleType }}
            </el-tag>
          </div>

          <!-- 车主信息 -->
          <div class="info-group">
            <h3 class="info-title">车主信息</h3>
            <div class="info-grid">
              <div class="info-item">
                <span class="label">车主姓名：</span>
                <span class="value">{{ vehicleInfo.ownerName }}</span>
              </div>
              <div class="info-item">
                <span class="label">手机号码：</span>
                <span class="value">{{ vehicleInfo.ownerPhone }}</span>
              </div>
            </div>
          </div>

          <!-- 出入信息 -->
          <div class="info-group">
            <h3 class="info-title">出入信息</h3>
            <div class="info-grid">
              <div class="info-item">
                <span class="label">出入小区：</span>
                <span class="value">{{ vehicleInfo.community }}</span>
              </div>
              <div class="info-item">
                <span class="label">入场时间：</span>
                <span class="value">{{ vehicleInfo.entryTime }}</span>
              </div>
              <div class="info-item">
                <span class="label">离场时间：</span>
                <span class="value">{{ vehicleInfo.exitTime }}</span>
              </div>
              <div class="info-item">
                <span class="label">出入闸机：</span>
                <span class="value">{{ vehicleInfo.gateDevice }}</span>
              </div>
            </div>
          </div>

          <!-- 费用信息 -->
          <div class="info-group">
            <h3 class="info-title">费用信息</h3>
            <div class="info-grid">
              <div class="info-item">
                <span class="label">停车时长：</span>
                <span class="value parking-duration">{{ vehicleInfo.parkingDuration }}</span>
              </div>
              <div class="info-item">
                <span class="label">停车费用：</span>
                <span class="value parking-fee">{{ vehicleInfo.parkingFee }}</span>
              </div>
              <div class="info-item">
                <span class="label">支付方式：</span>
                <el-tag :type="getPaymentType(vehicleInfo.paymentMethod)" class="payment-tag">
                  {{ vehicleInfo.paymentMethod }}
                </el-tag>
              </div>
            </div>
          </div>
        </div>

        <!-- 右侧图片区域 -->
        <div class="image-section">
          <div class="image-container">
            <!-- 显示传入的图片 -->
            <div v-if="vehicleInfo.image" class="image-preview">
              <el-image
                  :src="vehicleInfo.image.url"
                  :alt="vehicleInfo.image.name"
                  fit="cover"
                  class="vehicle-image"
                  :preview-src-list="[vehicleInfo.image.url]"
                  hide-on-click-modal
              >
                <template #error>
                  <div class="image-error">
                    <el-icon><Picture /></el-icon>
                    <span>图片加载失败</span>
                  </div>
                </template>
              </el-image>
              <div class="image-name">{{ vehicleInfo.image.name }}</div>
            </div>

            <!-- 无图片时的空状态 -->
            <div v-else class="empty-state">
              <el-icon size="36" color="#c0c4cc"><Picture /></el-icon>
              <p>暂无照片</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted,watch } from 'vue'
import { Picture } from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'


// 定义props
const props = defineProps({
  // 车辆信息对象
  vehicleData: {
    type: Object,
    default: () => ({})
  }
})

const route = useRoute()

// 车辆信息数据
const vehicleInfo = ref({})

// 根据支付方式获取标签类型
const getPaymentType = (method) => {
  const typeMap = {
    '月卡': 'success',
    '临时': 'warning',
    '年卡': 'info',
    '未支付': '',
    '免费': '',
    '微信': 'success',
    '支付宝': 'primary'
  }
  return typeMap[method] || ''
}

// 从车牌号中提取前缀和主要部分
// 从车牌号中提取前缀和主要部分
const parsePlateNumber = (carNo) => {
  if (!carNo) return { prefix: '', main: '' }

  console.log('原始车牌号:', carNo)

  // 处理各种车牌号格式
  if (carNo.length === 8) {
    // 新能源车牌：前2位省份+1位字母+后面5位
    return { prefix: carNo.substring(0, 3), main: carNo.substring(3) }
  } else if (carNo.length === 7) {
    // 普通车牌：前2位省份+后面5位
    return { prefix: carNo.substring(0, 2), main: carNo.substring(2) }
  } else {
    // 其他格式，直接显示
    return { prefix: '', main: carNo }
  }
}

// 格式化显示时间
const formatDisplayTime = (timeString) => {
  if (!timeString) return '-'
  try {
    const date = new Date(timeString)
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    }).replace(/\//g, '/')
  } catch (e) {
    console.error('时间格式化错误:', e)
    return timeString
  }
}



// 初始化数据
const initVehicleInfo = (query) => {
  // 从路由参数获取数据
  const plateInfo = parsePlateNumber(query.carNo)

  // 处理图片URL
  let imageData = null
  if (query.imageUrl && query.imageUrl !== 'null' && query.imageUrl !== 'undefined') {
    imageData = {
      url: query.imageUrl,
      name: '出入照片'
    }
  }

  vehicleInfo.value = {
    platePrefix: plateInfo.prefix,
    plateNumber: plateInfo.main,
    vehicleType: query.vehicleType || '小型轿车',
    ownerName: query.ownerName || '未知',
    ownerPhone: query.ownerPhone || '未知',
    community: query.community || '未知小区',
    entryTime: formatDisplayTime(query.entryTime),
    exitTime: formatDisplayTime(query.exitTime),
    gateDevice: query.gateDevice || '未知闸机',
    parkingDuration: query.parkingDuration || '-',
    parkingFee: query.parkingFee ? `${query.parkingFee}元` : '0元',
    paymentMethod: query.paymentMethod || '未知',
    image: imageData
  }

  console.log('设置后的vehicleInfo:', vehicleInfo.value)
}


// 监听路由查询参数变化
watch(
    () => route.query,
    (newQuery) => {
      if (newQuery && Object.keys(newQuery).length > 0) {
        console.log('更新详情数据:', newQuery)
        initVehicleInfo(newQuery)
      }
    },
    { immediate: true, deep: true }
)
</script>

<style scoped>
.vehicle-detail-container {
  padding: 20px;
  background-color: #ffffff;
  min-height: 100vh;
}

.detail-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  border: 1px solid #e4e7ed;
  max-width: 1200px;
  margin: 0 auto;
}

.header {
  padding: 20px 30px;
  border-bottom: 1px solid #e4e7ed;
  background: white;
}

.header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.content-wrapper {
  display: flex;
  min-height: 500px;
}

/* 左侧信息区域样式 */
.info-section {
  flex: 3;
  padding: 24px;
  border-right: 1px solid #e4e7ed;
}

.plate-section {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #e4e7ed;
}

.plate-number {
  display: flex;
  align-items: center;
  background: #f5f7fa;
  padding: 10px 16px;
  border-radius: 6px;
  color: #303133;
  font-weight: bold;
  border: 1px solid #dcdfe6;
}

.plate-prefix {
  font-size: 16px;
  margin-right: 4px;
}

.plate-main {
  font-size: 18px;
  letter-spacing: 1px;
}

.vehicle-type-tag {
  font-size: 12px;
  padding: 4px 8px;
}

.info-group {
  margin-bottom: 24px;
}

.info-title {
  font-size: 15px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 12px;
  padding-left: 8px;
  border-left: 3px solid #409eff;
}

.info-grid {
  display: grid;
  gap: 12px;
}

.info-item {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  background: #f8f9fa;
  border-radius: 4px;
  min-height: 44px;
}

.info-item .label {
  font-weight: 500;
  color: #606266;
  min-width: 80px;
  font-size: 14px;
}

.info-item .value {
  color: #303133;
  font-weight: 500;
  font-size: 14px;
}

.parking-duration {
  color: #e6a23c;
}

.parking-fee {
  color: #f56c6c;
}

.payment-tag {
  font-weight: 500;
}

/* 右侧图片区域样式 */
.image-section {
  flex: 2;
  min-width: 300px;
  max-width: 400px;
  padding: 24px;
  background: white;
  display: flex;
  flex-direction: column;
}

.image-title {
  font-size: 15px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 16px;
  text-align: center;
}

.image-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 200px;
}

.image-preview {
  display: flex;
  flex-direction: column;
  height: 100%;
  gap: 12px;
}

.vehicle-image {
  width: 100%;
  height: 200px;
  border-radius: 6px;
  background: #f5f7fa;
  border: 1px solid #e4e7ed;
  object-fit: cover;
}

.image-name {
  text-align: center;
  font-size: 13px;
  color: #606266;
  font-weight: 500;
  padding: 4px 0;
}

.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #909399;
  gap: 8px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 200px;
  color: #c0c4cc;
  background: #fafafa;
  border-radius: 6px;
  border: 1px dashed #dcdfe6;
}

.empty-state p {
  margin-top: 8px;
  font-size: 13px;
  color: #909399;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .content-wrapper {
    flex-direction: column;
  }

  .info-section {
    border-right: none;
    border-bottom: 1px solid #e4e7ed;
  }

  .image-section {
    min-width: 100%;
    max-width: 100%;
    height: 250px;
  }

  .vehicle-image {
    height: 180px;
  }

  .empty-state {
    height: 180px;
  }
}
</style>